package com.originskyseed.platform.user.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.SalesmanLevel;
import com.originskyseed.platform.user.model.SalesmanRelateLevel;
import com.originskyseed.platform.user.model.SalesmanRelateLevelLog;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.ISalesmanLevelService;
import com.originskyseed.platform.user.service.ISalesmanRelateLevelService;
import com.originskyseed.platform.user.vo.request.SalesmanRelateLevelRequestVO;

/**
 * 业务员能量值Controller类（OSI_SALESMAN_RELATE_LEVEL）
 * 
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/salesmanRelateLevel")
@BaseLocaleResource(localeFileName = "i18n_user")
public class SalesmanRelateLevelController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SalesmanRelateLevelController.class);

	@DubboReference
	private ISalesmanRelateLevelService salesmanRelateLevelService;

	@DubboReference
	private ISalesmanLevelService salesmanLevelaService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/salesmanRelateLevelList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<SalesmanRelateLevel>> salesmanRelateLevelList(HttpServletRequest request) {
		BaseResponseBody<BasePage<SalesmanRelateLevel>> result = null;
		try {
			// 请求对象
			SalesmanRelateLevelRequestVO requestVO = this.getParameterBean(SalesmanRelateLevelRequestVO.class);

			// 分页查询
			BasePage<SalesmanRelateLevel> page = new BasePage<SalesmanRelateLevel>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.salesmanRelateLevelService.selectListRelLevelByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<SalesmanRelateLevel>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<SalesmanRelateLevel>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/salesmanRelateLevelSave.json", method = RequestMethod.POST)
	public BaseResponseBody<SalesmanRelateLevel> salesmanRelateLevelSave(HttpServletRequest request) {
		BaseResponseBody<SalesmanRelateLevel> result = null;
		try {
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 请求对象
			SalesmanRelateLevel salesmanRelateLevel = this.getParameterBean(SalesmanRelateLevel.class);

			if (StringUtils.isNotBlank(salesmanRelateLevel.getId())) {
				this.salesmanRelateLevelService.updateByPrimaryKey(salesmanRelateLevel);
			} else {
				this.salesmanRelateLevelService.insertSelective(salesmanRelateLevel);
			}
			result = new BaseResponseBody<SalesmanRelateLevel>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), salesmanRelateLevel);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<SalesmanRelateLevel>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 跳转能量值接口
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateEmpiricValue.json", method = RequestMethod.POST)
	public BaseResponseBody updateEmpiricValue(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 请求对象
			SalesmanRelateLevel salesmanRelateLevel = this.getParameterBean(SalesmanRelateLevel.class);

			// 判断根据经验值是否能查询到对应的等级
			String levelId = this.salesmanLevelaService.selectLevelIdByEmpiricValue(salesmanRelateLevel.getEmpiricValue(),null);
			if (StringUtils.isEmpty(levelId)) {
				return new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.NOT_FIND_SALESMAN_LEVEL_ERROR.getMsg()), false);
			}

			SalesmanRelateLevel oldSalesmanRelateLevel = this.salesmanRelateLevelService.selectByPrimaryKey(salesmanRelateLevel.getId());
			SalesmanLevel oldSalesmanLevela = this.salesmanLevelaService.selectByPrimaryKey(oldSalesmanRelateLevel.getLevelId());
			SalesmanLevel salesmanLevela = this.salesmanLevelaService.selectByPrimaryKey(levelId);

			SalesmanRelateLevelLog salesmanRelateLevelLog = new SalesmanRelateLevelLog();
			salesmanRelateLevelLog.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
			salesmanRelateLevelLog.setCreator(userInfo.getName());
			salesmanRelateLevelLog.setCreatorId(userInfo.getId());
			salesmanRelateLevelLog.setBusinessId(salesmanRelateLevel.getId());
			salesmanRelateLevelLog.setExpValue(salesmanRelateLevel.getEmpiricValue());
			salesmanRelateLevelLog.setOldExpValue(oldSalesmanRelateLevel.getEmpiricValue());
			salesmanRelateLevelLog.setLevelName(salesmanLevela.getLevelName());
			if (oldSalesmanLevela != null) {
				salesmanRelateLevelLog.setOldLevelName(oldSalesmanLevela.getLevelName());
			}
			this.salesmanRelateLevelService.updateByPrimaryKey(salesmanRelateLevel, salesmanRelateLevelLog);

			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), salesmanRelateLevel);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/salesmanRelateLevelDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> salesmanRelateLevelDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.salesmanRelateLevelService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}